class JnlpController < ApplicationController
  
  def make
    sfilename=Time.now.to_f.to_s+rand(0xFFFFFFFF).to_s+".jnlp"
    
    if(params[:port].blank?)
      params[:port]="443"
    end
    if($cache.nil?)then
      @content=File.open("public/kerjodando.jnlp"){|f|f.read}
      logger.warn("the message should be show once in around-running")
      @content.gsub!('$$title','dargens')
      $cache=@content
    end
    str=request.protocol << request.host_with_port 
    @content=$cache.gsub('$$codebase',str+"/kerjodando/")
    @content.gsub!('$$href','')
    
    s=User.find(session[:user_id])
    if(s.nil?)||(session[:user_id].nil?) then redirect_to :controller=>"index" end
    @content.gsub!('$$icon',(str+'/kerjodando/kerjodando.jpg'))
    
    replace_str = getXmlproperty('user_public_ip',request.remote_ip)
    
    request.parameters.each {|key,value|replace_str<<getXmlproperty(key,value)}
    #cm=ConnectionManager.new
    #cm.build_mynet(s.id,request.remote_ip,params[:port],nil,session.session_id.to_s)
    replace_str << getXmlproperty("identifier",session[:user_id])
    response.headers["Content-Type"] = 'application/x-java-jnlp'
    response.headers["Cache-Control"]='private,post-check=90,pre-check=100'
    response.headers["pragma"]='no-cache'
    response.headers["Expires"]='0'
    response.headers["Content-Disposition"]='filename='+sfilename

    @content.gsub!('$$properties',replace_str)
    render :layout=>false
  
  end
  def getXmlproperty(name,value)
  	value=(request.protocol << request.host_with_port)+'/trustedpeers' if name=="server"
    "<property name=\"" + name + "\" value=\"" + value + "\"/>\n";
  end
end
